/*
 * Filter (Refactored)@VERSION
 *
 * Copyright XXXX, COPYRIGHT HOLDERS
 *
 * Depends:
 * 
 */

var API = API || {};

API.x_filter = {
	FilterManager :
		/**
		 * @param filter_trigger	element to attach trigger event
		 * @param search_list		results container
		 * @param references		list of components reference
		 */
		function(filter_trigger, search_list, result_items, sort_by_container, references) {
			var parent = this;
	
			// Manager objects
			this.effects_manager = references.effects_manager;
			this.sorter_manager = references.sorter_manager;
			this.slider_manager = references.slider_manager;
			this.pager_manager = references.pager_manager;
	
			// private variables
			this.filter_trigger = filter_trigger;
			this.search_list = search_list;
			this.result_items = result_items;
			this.sort_by_container = sort_by_container;
			
			this.sort_by_selected  = null;

			this.pager_manager.Init();
			this.effects_manager.Init(search_list, this.pager_manager.getContainerId());
			
			var onTriggerClickHandler = function() {
				if(is_filtering === true){
					return false;
				} else {
					is_filtering = true;
				}
				
				parent.effects_manager.preEffects();
				// Toggle activity state
				if ($(this).hasClass('inactive')) {
					$(this).removeClass('inactive');
					$(this).addClass('active');
				} else {
					$(this).removeClass('active');
					$(this).addClass('inactive');
				}
	
				var category = $(this).attr('data-category');
				var active = $(this).hasClass('active');
				var id = $(this).attr('data-id');
	
				parent.effects_manager.afterEffects(parent.doFilter, [category, active, id]);
			};
	
			 this.Data = {
				facility_id : new Array(),
				price_id : new Array(),
				stars_id : new Array(),
				distances_sea_id : new Array(),
				distances_center_id : new Array(),
				facility_item : new Array(),
				price_item : new Array(),
				stars_item : new Array(),
				distances_sea_item : new Array(),
				distances_center_item : new Array(),
				listElems : new Array(),
				sortBy : 'recommended'
			};
	
			this.Section = {
				Facility : function() {
					parent.Data.facility_id = new Array();
					$.each(parent.result_items, function(n, item) { // idemo kroz objekte
	
						var count_item = parent.Data.facility_item.length;  // broji duzinu ukljucenih filtera
						var cnt = 0; // brojac pronadjenih filtera
	
						if ((typeof this.all_attributes !== 'undefined')
								&& (this.all_attributes !== null)) {
	
							$.each(this.all_attributes, function(attribute_id, n) { // idemo kroz pripremljene atribute
	
								$.each(parent.Data.facility_item, function(n,
										item_id) { // idemo kroz ukljucene filter
													// atribute
	
									if (attribute_id === item_id)
										cnt++;
								});
							});
							if (count_item === cnt) {
								parent.Data.facility_id
										.push(parseInt(item.object_ID));
								// log('PUSH');
							}
	
						}
					});
				},
				Price : function() { // Bilo koja vrijednost iz price_item ako je nadjena, ukljucujemo taj objekat
	
					parent.Data.price_id = new Array();
					cnt = 0;
					$.each( parent.result_items, function(n, item) { // idemo kroz objekte
	
						// if (cnt == 4)
						// return;
						//
						// cnt++;
	
						var OBJECT_FOUND = false;
						$.each(parent.Data.price_item, function(n, rng) { // idemo kroz price range
	
							if (!OBJECT_FOUND) { // ovo koristimo umjesto izlaska break 2;
	
								var prc_range = rng
										.split(",");
								if (prc_range[1] === '-1')
									prc_range[1] = 999999;
								if (!preview) {
	
									// log('not_preview')
	
									$.each(item.rooms, function( n, room) { // idemo  kroz sobe
	
										var price = parseFloat(room.selling_price);
										if ((price >= prc_range[0])
												&& (price < prc_range[1])) {
	
											parent.Data.price_id
													.push(parseInt(item.object_ID));
											OBJECT_FOUND = true;
											return false; // ovdje izlazimo  iz soba
										}
	
									});
								} else {
	
									// log('is_preview')
	
									var price = parseFloat(item.lowest_price);
									if ((price >= prc_range[0]) && (price < prc_range[1])) {
	
										parent.Data.price_id
												.push(parseInt(item.object_ID));
										OBJECT_FOUND = true;
										return false; // ovdje izlazimo iz soba
	
									}
	
								}
							}
	
						});
					});
				},
				Stars : function() {
	
					parent.Data.stars_id = new Array();
					$.each(parent.result_items, function(n, item) { // idemo kroz objekte
	
						if (item.object_number_of_stars == "")
							item.object_number_of_stars = 0;
						$.each(parent.Data.stars_item,
								function(n, star) { // idemo kroz stars_item
	
									if (star == item.object_number_of_stars) {
										parent.Data.stars_id
												.push(parseInt(item.object_ID));
										return false; // ovdje izlazimo iz stars_item
									}
	
								});
					});
				},
				Distances : function(attribute, distance) {
	
					if (attribute === 'object_distance_from_the_sea') {
						parent.Data.distances_sea_id = new Array();
					} else {
						parent.Data.distances_center_id = new Array();
					}
	
					$.each(parent.result_items, function(n, item) { // idemo kroz objekte
	
						if (item[attribute] === "")
							item[attribute] = 999999;
						if (parseInt(distance) >= parseInt(item[attribute])) {
	
							if (attribute === 'object_distance_from_the_sea') {
								parent.Data.distances_sea_id
										.push(parseInt(item.object_ID));
							} else {
								parent.Data.distances_center_id
										.push(parseInt(item.object_ID));
							}
						}
					});
				}
			};
	
			this.doFilter = function(category, active, id) {
				switch (category) {
	
				case 'filter_facilities':
	
					if (active) {
						parent.Data.facility_item.push(id);
					} else {
						parent.Data.facility_item.splice($.inArray(id, parent.Data.facility_item), 1);
					}
	
					if (parent.Data.facility_item.length > 0) {
						parent.Section.Facility();
					} else {
						parent.Data.facility_id = new Array();
					}
	
					break;
				case 'filter_price':
	
					if (active) {
						parent.Data.price_item.push(id);
					} else {
						parent.Data.price_item.splice($.inArray(id, parent.Data.price_item), 1);
					}
	
					if (parent.Data.price_item.length > 0) {
						parent.Section.Price();
					} else {
						parent.Data.price_id = new Array();
					}
	
					break;
				case 'filter_stars':
	
					if (active) {
						parent.Data.stars_item.push(id);
					} else {
						parent.Data.stars_item.splice($.inArray(id, parent.Data.stars_item), 1);
					}
	
					if (parent.Data.stars_item.length > 0) {
						parent.Section.Stars();
					} else {
						parent.Data.stars_id = new Array();
					}
	
					break;
				case 'beach_distance':
	
					if (active) {
						parent.Data.distances_sea_item[0] = id;
						parent.Section.Distances('object_distance_from_the_sea',
								parent.Data.distances_sea_item[0]);
					} else {
						parent.Data.distances_sea_id = new Array();
						parent.Data.distances_sea_item = new Array();
					}
	
					break;
				case 'center_distance':
	
					if (active) {
						parent.Data.distances_center_item[0] = id;
						parent.Section.Distances('object_distance_from_the_center',
								parent.Data.distances_center_item[0]);
					} else {
						parent.Data.distances_center_id = new Array();
						parent.Data.distances_center_item = new Array();
					}
	
					break;
				} // end switch
	
				parent.DoAllArrays();
			};
	
			this.DoAllArrays = function() {
				var all_id = [ parent.Data.facility_id, parent.Data.price_id,
						parent.Data.stars_id, parent.Data.distances_sea_id,
						parent.Data.distances_center_id ];
				var all_item = [ parent.Data.facility_item, parent.Data.price_item,
						parent.Data.stars_item, parent.Data.distances_sea_item,
						parent.Data.distances_center_item ];
				var selected = new Array();
				var final = new Array();
				_.each(all_item, function(arr, index) {
	
					if (arr.length > 0)
						selected.push(all_id[index]);
				});
				if (selected.length > 1) {
					// log('intersection')
					final = _.intersection.apply(_, selected);
				} else if (selected.length === 1) {
					// log('equals');
					final = selected[0];
				} else {
					final = new Array();
				}
	
				var myItems = new Array();
				if (selected.length > 0) {
	
					$(parent.search_list + ' li').remove();
	
					$.each(parent.result_items, function(i, item) { // idemo kroz objekti niz
	
						var ID = parseInt(item.object_ID);
	
						if ($.inArray(ID, final) !== -1) {
							myItems.push(item);
						}
	
					});
	
				} else {
					// $(parent.search_list).html(parent.Data.listElems);
					myItems = parent.result_items.slice(0);
				}
	
				parent.sorter_manager.sort(myItems);
	
				$.each(myItems, function(i, item) { // idemo kroz objekti niz
	
					var ID = parseInt(item.object_ID);
	
					$(parent.search_list).append(parent.Data.listElems[ID]);
				});
	
			    var result_count = Object.keys(myItems).length;
			    if(result_count === 0){
			    	$(parent.search_list).append('<li><div style=\"padding: 15px\">No results ...</div></li>');
			    }
				// $('.hotels-count').text($('#list li').length);
	
				parent.pager_manager.Init();

				// Hover Thumbnails
				$("a.HoverImg").thumbPopup({});
			};
	
			this.initialize = function() {
				parent.filter_trigger.on('click', onTriggerClickHandler);

				// TODO: Is this needed?
				if ($(window).width() > 900) {
	
					var height = $('.mask-height').height();
					$('.results-holder').height(height);
					$(parent.search_list).height(height);
				}

				$(sort_by_container).on('change', function() {
					if(is_filtering == true){
						$(parent.sort_by_selected).prop("selected", true);
						
						return false;
					} else {
						is_filtering = true;
					}
					parent.effects_manager.preEffects();
					parent.effects_manager.afterEffects(parent.DoAllArrays);
				});

				$(sort_by_container).on('click', function() {
					parent.sort_by_selected = $(sort_by_container + ' option:selected');

				});
				
				$(parent.search_list + " li").each(function(index, value) {
					var ID = $(this).attr('data-id');
					parent.Data.listElems[ID] = value;
				});
				
				parent.slider_manager.Init(parent.doFilter);
				
			};
		}
};